package com.example.myapp_iphoneicon;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;

public class LayoutCalculator {
    public static final int BLACKCIRCLE_RADIUS = 13;
    public static int DOCKBAR_HEIGHT = 80;
    public static final int ICON_HEIGHT = 60;
    public static final int ICON_WIDTH = 60;
    public static final int PAGER_GAP = 10;
    public static final int PAGER_HEIGHT = 16;// fix bug SSIXTEEN-61, rancaihe,
                                                // 20120907
    public static final int REDCIRCLE_MARGIN_TOP = 6;
    public static final int REDCIRCLE_RADIUS = 12;
    public static final int WALLPAPER_SHADOW_ADJUSTMENT = 16;
    public static int sColumns = 4;
    public static int sDockColumns = 4;
    public static int sIconsPerPage;
    public static int sRows = 5;
    private WeakReference<Activity> mActivity;
    public int mBcMarginLeft;
    public int mBcMarginTop;
    public int mCMarginTop;
    private int mDensity;
    public int mFullMarginLeft;
    public int mFullMarginLeftBlackCircle;
    public int mFullMarginRight;
    public int mGapDockH;
    public int mGapFolderV;
    public int mGapH;
    public int mGapV;
    public int mHeight;
    public int mIconHeight;
    public int mIconLastMarginRight;
    public int mIconMarginLeft;
    public int mIconMarginLeftBlackCircle;
    public int mIconWidth;
    public boolean mIsHighResolution;
    public int mItemHeight;
    public int mMarginLeft;
    public int mMarginLeftDock;
    public int mMarginTop;
    public int mPageHeight;
    public int mPagerGap;
    public int mRcMarginRight;
    public int mRcMarginTop;
    private float mScale;
    public int mShadowMarginBottom;
    public int mShadowMarginLeft;
    public int mShadowMarginRight;
    public int mTextTop;
    public int mWidth;
    public Resources mResources;

    public LayoutCalculator(Activity mActv) {
        mActivity = new WeakReference<Activity>(mActv);
        mResources = mActv.getResources();
        DisplayMetrics dm = mResources.getDisplayMetrics();
        mScale = dm.density;
        mDensity = dm.densityDpi;
        mIconWidth = 140;//getDimension(R.dimen.app_icon_width);
        mIconHeight = 140;//getDimension(R.dimen.app_icon_height);
        DOCKBAR_HEIGHT = 140;//getDimension(R.dimen.dockbar_height);
        mTextTop = mIconHeight + dpToPixel(BLACKCIRCLE_RADIUS);
        mItemHeight = mIconHeight + dpToPixel(WALLPAPER_SHADOW_ADJUSTMENT);
        mBcMarginLeft = dpToPixel(BLACKCIRCLE_RADIUS) - dpToPixel(1);
        mBcMarginTop = mBcMarginLeft;
        mRcMarginTop = dpToPixel(REDCIRCLE_MARGIN_TOP);
        mRcMarginRight = mRcMarginTop;
        mCMarginTop = Math.max(mBcMarginTop, mRcMarginTop);
        mPagerGap = dpToPixel(PAGER_GAP);
    }

    public float dpToPixel(float paramFloat) {
        return mScale * paramFloat;
    }

    public int dpToPixel(int paramInt) {
        return (int) (paramInt * mScale + 0.5F);
    }

    public int getBlackCircleMarginLeft() {
        return mBcMarginLeft;
    }

    public int getBlackCircleMarginTop() {
        return mBcMarginTop;
    }

    public int getDensity() {
        return mDensity;
    }

    public int getDockbarVitalLeft() {
        return mMarginLeftDock + mGapDockH - mFullMarginLeft;
    }

    public int getDockbarVitalWidth() {
        return mFullMarginLeft
                + mIconWidth
                * sDockColumns
                + mGapDockH
                * (sDockColumns - 1)
                + Math.max(Math.max(mShadowMarginRight, mRcMarginRight),
                        mIconMarginLeft);
    }

    public int getFullItemHeight() {
        return mRcMarginTop + mItemHeight + mShadowMarginBottom;
    }

    public int getFullItemWidth() {
        return mFullMarginLeft + mFullMarginLeft
                + Math.max(mIconLastMarginRight, mShadowMarginRight);
    }

    public int getFullItemWidthBlackCircle() {
        return mFullMarginLeftBlackCircle + mIconWidth + mFullMarginRight;
    }

    public int getHorizontalGap() {
        return mGapH;
    }

    public int getIconHeight() {
        return mIconHeight;
    }

    public Point getIconLocation(int paramInt) {
        Point localPoint = new Point();
        localPoint.x = mMarginLeft + mGapH + (mIconWidth + mGapH)
                * (paramInt % sColumns);
        localPoint.y = mMarginTop + mGapV + (mItemHeight + mGapV)
                * (paramInt / sColumns);
        return localPoint;
    }

    public int getIconMarginLeft() {
        return mIconMarginLeft;
    }

    public int getIconWidth() {
        return mIconWidth;
    }

    public int getItemHeight() {
        return mItemHeight;
    }

    public int getItemWidth() {
        return mIconMarginLeft + mIconWidth + mIconLastMarginRight;
    }

    public int getItemWidthBlackCircle() {
        return mIconMarginLeftBlackCircle + mIconWidth + mIconLastMarginRight;
    }

    public int getMarginLeft() {
        return mMarginLeft;
    }

    public int getMarginTop() {
        return mMarginTop;
    }

    public int getPageHeight() {
        return mPageHeight;
    }

    public int getRedCircleMarginRight() {
        return mRcMarginRight;
    }

    public int getTextTop() {
        return mTextTop;
    }

    public int getVerticalGap() {
        return mGapV;
    }

    public int getVitalHeight() {
        return mRcMarginTop + mItemHeight * sRows + mGapV * (sRows - 1)
                + mShadowMarginBottom;
    }

    public int getVitalLeft() {
        return mMarginLeft + mGapH - mFullMarginLeft;
    }

    public int getVitalTop() {
        return mMarginTop + mGapV - mRcMarginTop;
    }

    public int getVitalWidth() {
        return mFullMarginLeft
                + mIconWidth
                * sColumns
                + mGapH
                * (sColumns - 1)
                + Math.max(Math.max(mShadowMarginRight, mRcMarginRight),
                        mIconMarginLeft);
    }

    public int getWidth() {
        return mWidth;
    }

/*    public void layoutReady(ViewGroup frame, View v, ObjectPool op) {
        Activity activity = mActivity.get();// v0
        if (activity == null) {
            return;
        }
        int statusHeight = 0;
        try {
            Class localclass = Class.forName("com.android.internal.R$dimen");
            Object obj = localclass.newInstance();
            Field field = localclass.getField("status_bar_height");
            statusHeight = frame.getResources().getDimensionPixelSize(
                    Integer.parseInt(field.get(obj).toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        mWidth = frame.getWidth();

        mHeight = frame.getHeight();

        int space = mWidth - (mIconWidth * sColumns);// v5

        mGapH = space / (sColumns + 1);
        mMarginLeft = space % (sColumns + 1) / 2;
        mGapDockH = space / (sDockColumns + 1);
        mMarginLeftDock = space % (sDockColumns + 1) / 2;
        mIconMarginLeft = Math.min(mIconWidth / 4, mGapH / 3);
        mIconMarginLeftBlackCircle = Math.max(mIconMarginLeft, mBcMarginLeft);
        mIconLastMarginRight = Math.max(mIconMarginLeft, mRcMarginRight);

        if (mGapH > mIconWidth) {
            mIsHighResolution = true;
        } else {// cond_1
            mIsHighResolution = false;
        }
        sIconsPerPage = sRows * sColumns;

        space = mHeight - DOCKBAR_HEIGHT - dpToPixel(PAGER_HEIGHT)
                - (mItemHeight * sRows);

        mGapV = space / (sRows + 2);
        mGapFolderV = dpToPixel(PAGER_GAP);
        ;
        mMarginTop = space % (sRows + 2) / 2;
        mPageHeight = mHeight - DOCKBAR_HEIGHT - mGapV
                - dpToPixel(PAGER_HEIGHT);

        int shadowMargin = op.getWallpaperIconShadow().getWidth() - mIconWidth;// v4

        mShadowMarginLeft = shadowMargin / 2;
        mShadowMarginRight = shadowMargin - mShadowMarginLeft;
        mShadowMarginBottom = Math.max(
                ((mIconHeight - dpToPixel(WALLPAPER_SHADOW_ADJUSTMENT)) + op
                        .getWallpaperIconShadow().getHeight() / 2)
                        - mItemHeight, 0);
        mFullMarginLeft = Math.max(mShadowMarginLeft, mIconMarginLeft);
        mFullMarginRight = Math.max(
                Math.max(mShadowMarginRight, mIconMarginLeft), mRcMarginRight);
        mFullMarginLeftBlackCircle = Math.max(mFullMarginLeft, mBcMarginLeft);
    }*/

    public int reflect(Canvas paramCanvas, int paramInt1, int paramInt2) {
        int m = paramInt2 + (mIconHeight - dpToPixel(3)) * 2;
        Rect localRect = new Rect(paramInt1, mIconHeight + paramInt2
                - dpToPixel(2), mIconWidth + paramInt1, m);
        paramCanvas.clipRect(localRect);
        paramCanvas.scale(1.0F, -1.0F, 0.0F, m);
        return m;
    }

    public Rect toItemRect(int paramInt1, int paramInt2) {
        Rect localRect = new Rect();
        localRect.left = paramInt1 - mIconMarginLeft;
        localRect.right = mIconWidth + paramInt1 + mIconMarginLeft
                + dpToPixel(13) / 2;
        localRect.top = paramInt2 - dpToPixel(13) / 2;
        localRect.bottom = getItemHeight() + paramInt2;
        return localRect;
    }

    public int getDimension(int resId) {
        return mResources.getDimensionPixelSize(resId);
    }

    public int getInteger(int resId) {
        return mResources.getInteger(resId);
    }
}
